package top.kaoshanji.p1ch03b;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.StandardProtocolFamily;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.Date;

/**
 * 代码清单 p1ch03b-15：进行组播的服务器端实现
 * author kaoshanji
 * ^_^
 * create time 2025/1/12 16:31
 */
public class DatagramChannelExample01 {

    private static final Logger logger = LogManager.getLogger(DatagramChannelExample01.class);

    public static void main(String[] args) throws IOException {
        logger.info("...代码清单 p1ch03b-15：进行组播的服务器端实现...");

        DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET).bind(null);
        InetAddress group = InetAddress.getByName("224.0.0.2");
        int port = 5000;
        while (true) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                break;
            }

            String str = (new Date()).toString();
            dc.send(ByteBuffer.wrap(str.getBytes()), new InetSocketAddress(group, port));

        }

    }

}
